package com.party.mobile.biz.analyze;

import com.google.common.base.Strings;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.MemberMerchantCount;
import com.party.core.model.member.MemberMerchantDetail;
import com.party.core.service.member.IMemberMerchantCountService;
import com.party.core.service.member.IMemberMerchantDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * Created by wei.li.
 *
 * @date 2017/12/14 0014
 * @time 19:32
 */

@Service
public class MemberMerchantDetailBizService {

    @Autowired
    private IMemberMerchantDetailService memberMerchantDetailService;

    @Autowired
    private IMemberMerchantCountService memberMerchantCountService;

    /**
     * @param orgId   机构id
     * @param payment 金额
     * @return
     * @Author yifeng
     * @Description 活动报名成功的处理合作商数据
     * @Date 13:56 2019/4/10
     **/
    public void updateActivityPaySuccess(String orgId, Float payment) {
        if (StringUtils.isEmpty(orgId)) {
            return;
        }
        MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(orgId);
        // 订单数
        memberMerchantCount.setOrderNum(memberMerchantCount.getOrderNum() + 1);

        // 累计金额
        double addCount = BigDecimalUtils.add(memberMerchantCount.getMoney(), payment);
        double rountCount = BigDecimalUtils.round(addCount, 2);
        memberMerchantCount.setMoney(rountCount);

        // 报名数
        memberMerchantCount.setApplyNum(memberMerchantCount.getApplyNum() + 1);

        memberMerchantCountService.update(memberMerchantCount);

        MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(orgId, DateUtils.today());
        // 订单数
        memberMerchantDetail.setOrderNum(memberMerchantDetail.getOrderNum() + 1);

        // 累计金额
        double add = BigDecimalUtils.add(memberMerchantDetail.getMoney(), payment);
        double round = BigDecimalUtils.round(add, 2);
        memberMerchantDetail.setMoney(round);

        // 报名数
        memberMerchantDetail.setApplyNum(memberMerchantDetail.getApplyNum() + 1);

        memberMerchantDetailService.update(memberMerchantDetail);
    }

    /**
     * @param orgId   机构id
     * @param payment 金额
     * @return
     * @Author yifeng
     * @Description 众筹成功的处理合作商数据
     * @Date 17:20 2019/4/11
     **/
    public void updateCrowdfundSuccess(String orgId, Float payment) {
        if (StringUtils.isEmpty(orgId)) {
            return;
        }
        MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(orgId);

        memberMerchantCount.setOrderNum(memberMerchantCount.getOrderNum() + 1);

        double addCount = BigDecimalUtils.add(memberMerchantCount.getMoney(), payment);
        double rountCount = BigDecimalUtils.round(addCount, 2);
        memberMerchantCount.setMoney(rountCount);

        memberMerchantCount.setSuccessNum(memberMerchantCount.getSuccessNum() + 1);

        memberMerchantCountService.update(memberMerchantCount);

        MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(orgId, DateUtils.today());
        memberMerchantDetail.setOrderNum(memberMerchantDetail.getOrderNum() + 1);

        double add = BigDecimalUtils.add(memberMerchantDetail.getMoney(), payment);
        double round = BigDecimalUtils.round(add, 2);
        memberMerchantDetail.setMoney(round);

        memberMerchantDetail.setSuccessNum(memberMerchantDetail.getSuccessNum() + 1);

        memberMerchantDetailService.update(memberMerchantDetail);
    }

    /**
     * 跟新报名数
     *
     * @param memberId 会员编号
     */
    public void updateApplyNum(String memberId) {
        if (!Strings.isNullOrEmpty(memberId)) {
            MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(memberId);
            if (null != memberMerchantCount.getApplyNum()) {
                memberMerchantCount.setApplyNum(memberMerchantCount.getApplyNum() + 1);
            } else {
                memberMerchantCount.setApplyNum(1);
            }
            memberMerchantCountService.update(memberMerchantCount);

            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(memberId, DateUtils.today());
            if (null != memberMerchantDetail.getApplyNum()) {
                memberMerchantDetail.setApplyNum(memberMerchantDetail.getApplyNum() + 1);
            } else {
                memberMerchantDetail.setApplyNum(1);
            }
            memberMerchantDetailService.update(memberMerchantDetail);

        }
    }

    /**
     * 更新众筹成功数
     *
     * @param memberId
     */
    public void updateSuccessNum(String memberId) {
        if (StringUtils.isEmpty(memberId)) {
            return;
        }
        MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(memberId);
        if (null != memberMerchantCount.getSuccessNum()) {
            memberMerchantCount.setSuccessNum(memberMerchantCount.getSuccessNum() + 1);
        } else {
            memberMerchantCount.setSuccessNum(1);
        }
        memberMerchantCountService.update(memberMerchantCount);

        MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(memberId, DateUtils.today());
        if (null != memberMerchantDetail.getSuccessNum()) {
            memberMerchantDetail.setSuccessNum(memberMerchantDetail.getSuccessNum() + 1);
        } else {
            memberMerchantDetail.setSuccessNum(1);
        }
        memberMerchantDetailService.update(memberMerchantDetail);
    }

    /**
     * 跟新订单和金额
     *
     * @param memberId 会员编号
     * @param payment  金额
     */
    public void updateOrderNum(String memberId, Float payment) {
        if (StringUtils.isEmpty(memberId)) {
            return;
        }
        MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(memberId);

        memberMerchantCount.setOrderNum(memberMerchantCount.getOrderNum() + 1);

        double addCount = BigDecimalUtils.add(memberMerchantCount.getMoney(), payment);
        double rountCount = BigDecimalUtils.round(addCount, 2);
        memberMerchantCount.setMoney(rountCount);

        memberMerchantCountService.update(memberMerchantCount);

        MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(memberId, DateUtils.today());
        memberMerchantDetail.setOrderNum(memberMerchantDetail.getOrderNum() + 1);

        double add = BigDecimalUtils.add(memberMerchantDetail.getMoney(), payment);
        double round = BigDecimalUtils.round(add, 2);
        memberMerchantDetail.setMoney(round);

        memberMerchantDetailService.update(memberMerchantDetail);
    }


    /**
     * 统计浏览量
     *
     * @param memberId 机构编号
     */
    @Transactional
    public void countViewNum(String memberId) {
        if (!Strings.isNullOrEmpty(memberId)) {
            int i = new Random().nextInt(10) + 1 + 10;
            //总计加
            MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(memberId);
            memberMerchantCount.setViewNum(memberMerchantCount.getViewNum() + (long) i);
            memberMerchantCountService.update(memberMerchantCount);

            //详情加
            Date today = DateUtils.today();
            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.getSet(memberId, today);
            memberMerchantDetail.setViewNum(memberMerchantDetail.getViewNum() + (long) i);
            memberMerchantDetailService.update(memberMerchantDetail);
        }
    }
}
